﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;

namespace Heng.Elements.Core.Model.Infrastructure.Messages
{
    /// <summary>
    /// Service response.
    /// </summary>
    [DataContract]
    public class ServiceResponse
    {
        #region Member Variables
        List<ServiceResponseMessage> _messages = null;
        #endregion

        #region Properties
        /// <summary>
        /// Whether the service operation was successful.
        /// </summary>
        [DataMember(Name = "success")]
        public bool Success { get; set; }

        /// <summary>
        /// Service operation messages.
        /// </summary>
        [DataMember(Name = "messages")]
        public List<ServiceResponseMessage> Messages
        {
            get
            {
                if (_messages == null)
                    _messages = new List<ServiceResponseMessage>();
                return _messages;
            }
        }
        #endregion

        public ServiceResponse Merge(ServiceResponse other)
        {
            this.Success = this.Success && other.Success;
            this.Messages.AddRange(other.Messages);

            return this;
        }
    }

    /// <summary>
    /// Service response.
    /// </summary>
    /// <typeparam name="TData"> Data type associated to service response. </typeparam>
    [DataContract]
    public class ServiceResponse<TData> : ServiceResponse
    {
        #region Properties
        /// <summary>
        /// Data associated to service response.
        /// </summary>
        [DataMember(Name = "data")]
        public TData Data { get; set; }
        #endregion

        public ServiceResponse<TData> Merge(ServiceResponse<TData> other)
        {
            base.Merge(other);
            this.Data = other.Data;

            return this;
        }
    }
}
